package com.szgr.baoyin.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.model.ForumListItem;
import com.szgr.baoyin.myview.CircleImageView;
import com.szgr.baoyin.utils.MyTool;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by yang on 15/9/22.
 */
public class MyForumListAdapter extends BaseAdapter {
    private Context mContext;
    private List<ForumListItem> data;
    private View.OnClickListener listener;
    public MyForumListAdapter(Context context,List<ForumListItem> data){
        mContext = context;
        this.data = data;
        listener = (View.OnClickListener) context;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_forum_list_view,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        ForumListItem forumListItem = data.get(position);
        holder.tv_name.setText(forumListItem.getUser_nick_name());

        MyTool.showPicture(mContext, holder.civ_head, forumListItem.getUserAvatar());
        holder.civ_head.setTag(position);
        holder.civ_head.setOnClickListener(listener);

//        long currentTime = System.currentTimeMillis() - Long.parseLong(forumListItem.getLast_reply_date());
//        System.out.println("---now time--->" + System.currentTimeMillis() + ":" + forumListItem.getLast_reply_date());
//        if(currentTime / 1000 < 60) {
//            holder.tv_time.setText((currentTime / 1000) + "秒前");
//        }else if(currentTime /(1000 * 60) < 60){
//            holder.tv_time.setText((currentTime /(1000 * 60)) + "分钟前");
//        }else if(currentTime /(1000 * 60 * 60) < 24){
//            holder.tv_time.setText((currentTime /(1000 * 60 * 60)) + "小时前");
//        }else{
//            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
//            String format = sdf.format(forumListItem.getLast_reply_date());
//            holder.tv_time.setText(format);
//        }
        String standardDate = MyTool.getStandardDate(forumListItem.getLast_reply_date());
        holder.tv_time.setText(standardDate);

        holder.tv_count.setText(forumListItem.getReplies());
        holder.tv_title.setText(forumListItem.getTitle());
        holder.tv_content.setText(forumListItem.getSubject());

        return convertView;
    }

    public void setData(List<ForumListItem> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    private class ViewHolder {
        CircleImageView civ_head;
        TextView tv_name;
        TextView tv_time;
        TextView tv_count;
        TextView tv_title;
        TextView tv_content;

        public ViewHolder(View view){
            civ_head = (CircleImageView) view.findViewById(R.id.item_forum_head_view);
            tv_name = (TextView) view.findViewById(R.id.item_forum_name);
            tv_time = (TextView) view.findViewById(R.id.item_forum_time);
            tv_count = (TextView) view.findViewById(R.id.item_msg_count);
            tv_title = (TextView) view.findViewById(R.id.item_forum_title);
            tv_content = (TextView) view.findViewById(R.id.item_forum_content);
        }
    }
}
